package CelestialDataAccess;

import javax.sql.rowset.*; //provides cached row set interface
import java.sql.*;

public class Product implements Comparable, IKey<String>{

    private String m_strProductID;
    private String m_strProductName;
    private String m_strCategory;
    private String m_strDescription;
    private String m_strMaterials;
    private String m_strWoodFinish;
    private String m_strImage1;
    
    private double m_dblLength;
    private double m_dblWidth;
    private double m_dblHeight;
    private double m_dblDiameter;
    
    private double m_dblPrice; // assume no rounding differences
    private int m_iUnits;
    private int m_iSold;
    
    private boolean m_bIsActive;

    private static IFactory<Product> m_factory = new IFactory<Product>(){

        public Product createT(CachedRowSet crs){

            Product prod;
            try{
                if(crs.getRow() < 1) return null;

                prod = new Product();
                prod.m_strProductID = crs.getString("ProductID");
                prod.m_strProductName = crs.getString("ProductName");
                prod.m_strCategory = crs.getString("Category");
                prod.m_strMaterials = crs.getString("Materials");
                prod.m_strWoodFinish = crs.getString("WoodFinish");
                prod.m_strDescription = crs.getString("Description");
                prod.m_strImage1 = crs.getString("Image1");
                
                prod.m_dblLength = crs.getDouble("Length");
                prod.m_dblWidth = crs.getDouble("Width");
                prod.m_dblHeight = crs.getDouble("Height");
                prod.m_dblDiameter = crs.getDouble("Diameter");

                prod.m_dblPrice = crs.getDouble("Price");
                prod.m_iUnits = crs.getInt("Units");
                prod.m_iSold = crs.getInt("Sold");
                
                prod.m_bIsActive = crs.getBoolean("IsActive");

            }catch(SQLException ex){
                return null;
            }
            return prod;
        }
    };

    /*************************************************************************/
    /* get and set methods */
    public String getProductID(){ return m_strProductID; }
    /*-----------------------------------------------------------------------*/
    public String getProductName(){ return m_strProductName; }
    public void setProductName(String prod_name){ m_strProductName = prod_name; }
    /*-----------------------------------------------------------------------*/
    public String getCategory(){ return m_strCategory; }
    public void settCategory(String category){ m_strCategory = category; }
    /*-----------------------------------------------------------------------*/
    public String getDescription(){ return m_strDescription; }
    public void setDescription(String desc){ m_strDescription = desc; }
    /*-----------------------------------------------------------------------*/
    public String getMaterials(){ return m_strMaterials; }
    public void setMaterials(String materials){ m_strMaterials = materials; }
    /*-----------------------------------------------------------------------*/
    public String getWoodFinish(){ return m_strWoodFinish; }
    public void setWoodFinish(String wood){ m_strWoodFinish = wood; }

    /*-----------------------------------------------------------------------*/
    public String getImage1(){ return m_strImage1; }
    public void setImage1(String image_path) { m_strImage1 = image_path; }
    /*-----------------------------------------------------------------------*/
    public double getLength(){ return m_dblLength; }
    public void setLength(double length) { m_dblLength = length; }
    /*-----------------------------------------------------------------------*/
    public double getWidth(){ return m_dblWidth; }
    public void setWidth(double width) { m_dblWidth = width; }
    /*-----------------------------------------------------------------------*/
    public double getHeight(){ return m_dblHeight; }
    public void setHeight(double height) { m_dblHeight = height; }
    /*-----------------------------------------------------------------------*/
    public double getDiameter(){ return m_dblDiameter; }
    public void setDiameter(double dia) { m_dblDiameter = dia; }

    /*-----------------------------------------------------------------------*/
    public double getPrice(){ return m_dblPrice; }
    public void setPrice(double price) { m_dblPrice = price; }
    /*-----------------------------------------------------------------------*/
    public int getUnits(){ return m_iUnits; }
    public void setUnits(int units){ m_iUnits = units; }
    /*-----------------------------------------------------------------------*/
    public int getSold(){ return m_iSold;}
    /*-----------------------------------------------------------------------*/
    public boolean isActive(){ return m_bIsActive; }
    public void setActive(boolean active){ m_bIsActive = active; }

    /*-----------------------------------------------------------------------*/
    static IFactory<Product> getFactory(){ return m_factory; }
    
    /*************************************************************************/
    /* Constructors */
    public Product(){ m_strProductID = "pro99999"; }
    /*-----------------------------------------------------------------------*/
    public Product(String id){ m_strProductID = id;}
    /*-----------------------------------------------------------------------*/
    static Product createProduct(CachedRowSet crs){
        return m_factory.createT(crs);
    }
    /*************************************************************************/
    /* IKey */
    public String getKey(){ return m_strProductID; }
    
    /*************************************************************************/
    /* IComparable */
    public int compareTo(Object prod){
        return m_strProductID.compareToIgnoreCase(((Product)prod).m_strProductID);
    }

    public String toString(boolean details){

        if(details)
            return m_strProductID + ";   " + m_strProductName + ";   " +
                   m_strCategory + ";   " + m_strDescription + ";   " +
                   m_strImage1  + ";   " + m_strMaterials + ";   " +
                   m_strWoodFinish + ";   " + m_dblPrice;
        else
            return toString();
    }
    /*-----------------------------------------------------------------------*/
    public String toString(){ return m_strProductID; }
}
